﻿using Autofac;
using EP.Base.BaseService.Manager.Business;
using EP.Base.BaseService.Manager.Business.Scheduler;
using EP.Base.BaseService.Manager.Business.Task;
using EP.Base.BaseService.Manager.Common;
using EP.Base.BaseService.Manager.Dal;
using EP.Base.BaseService.Manager.EsSystemData;
using EP.Base.BaseService.Manager.Interface;
using EP.Base.BaseService.Manager.Interface.Business;
using EP.Base.BaseService.Manager.Interface.Common;
using EP.Base.BaseService.Manager.Interface.Data;
using System;

namespace EP.Base.BaseService.Manager.Container
{
    public class AppContainer
    {
        public static IContainer _container;
        private static object sglock = new object();
        static AppContainer()
        {
            if (_container == null)
            {
                lock (sglock)
                {
                    if (_container == null)
                    {
                        var builder = GetContainerBuilder();
                        _container = builder.Build();
                    }
                }
            }
        }
        public static ContainerBuilder GetContainerBuilder(Action<ContainerBuilder> _register=null)
        {
            var containerBuilder = new ContainerBuilder();
            containerBuilder.Register(c => _container).As<IContainer>().SingleInstance();
            //common
            containerBuilder.RegisterType<Log4Net>().As<ILog>().SingleInstance();
            containerBuilder.RegisterType<ServiceMonitor>().As<IServiceMonitor>().SingleInstance();
            containerBuilder.RegisterType<ServiceSimplifyLog>().As<IServiceSimplifyLog>().SingleInstance();
            containerBuilder.RegisterType<Json>().As<IJson>().SingleInstance();
            containerBuilder.RegisterType<Email>().As<IEmail>().SingleInstance();
            //data
            containerBuilder.RegisterType<EsClusters>().As<IEsClusters>().SingleInstance();
            containerBuilder.RegisterType<EsTaskConfigs>().As<IEsTaskConfigs>().SingleInstance();
            containerBuilder.RegisterType<IndexConfigs>().As<IIndexConfigs>().SingleInstance();
            containerBuilder.RegisterType<IndexDeleteLogs>().As<IIndexDeleteLogs>().SingleInstance();
            containerBuilder.RegisterType<TaskRunLogs>().As<ITaskRunLogs>().SingleInstance();
            containerBuilder.RegisterType<IndexBackupLogs>().As<IIndexBackupLogs>().SingleInstance();
            containerBuilder.RegisterType<LogstashConfigLogs>().As<ILogstashConfigLogs>().SingleInstance();
            //business
            containerBuilder.RegisterType<EsIndexHandelTaskRepertory>().As<IEsIndexHandelTaskRepertory>().SingleInstance();
            containerBuilder.RegisterType<EsMaintainTask>().As<IEsMaintainTask>().SingleInstance();

            containerBuilder.RegisterType<EsServiceSchedulerAutoMonitor>()
                .As<IAutoMonitorService>()
                .Named<IAutoMonitorService>("EsServiceSchedulerAutoMonitor")
                .SingleInstance();
            containerBuilder.RegisterType<EsServiceSingleSchedulerAutoMonitor>()
                .As<IAutoMonitorService>()
                .Named<IAutoMonitorService>("EsServiceSingleSchedulerAutoMonitor")
                .SingleInstance();
            containerBuilder.RegisterType<IndexDateExtract>().As<IIndexDateExtract>().SingleInstance();
            //es imp
            containerBuilder.RegisterType<EsData>().As<IEsData>().SingleInstance();
                
            //task imp
            //待删除索引检测任务
            containerBuilder.RegisterType<CheckDeleteIndexTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("CheckDeleteIndexTask").SingleInstance();
            //索引备份任务
            containerBuilder.RegisterType<BackupIndexTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("BackupIndexTask").SingleInstance();
            //索引删除待确认通知任务
            containerBuilder.RegisterType<DlIndexConfirmRemindTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("DlIndexConfirmRemindTask").SingleInstance();
            //索引删除任务
            containerBuilder.RegisterType<DeleteIndexTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("DeleteIndexTask").SingleInstance();
            //索引备份状态检测任务
            containerBuilder.RegisterType<IndexBackupStateCheckTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("IndexBackupStateCheckTask").SingleInstance();
            //es备份删除任务
            containerBuilder.RegisterType<IndexBackupDeleteTask>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("IndexBackupDeleteTask").SingleInstance();

            //待删除索引检测任务_新的实现
            containerBuilder.RegisterType<CheckDeleteIndexTask_New>().As<IEsIndexHandelTask>().Named<IEsIndexHandelTask>("CheckDeleteIndexTask_New").SingleInstance();

            _register?.Invoke(containerBuilder);

            return containerBuilder;
        }

        public static T Resolve<T>()
        {
            return _container.Resolve<T>();
        }
        public static T Resolve<T>(string name)
        {
            return _container.ResolveNamed<T>(name);
        }
    }
}
